package com.action.user;

import com.db.user.*;
import com.db.admin.*;
import com.opensymphony.xwork2.*;
import java.util.*;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.omg.CORBA.Request;

public class UserLoginAction extends ActionSupport implements ModelDriven<UserVO>, Preparable, ServletRequestAware{
	// Spring Container로부터 주입받는 DAO객체
	private UserDAO userDAO;
	private UserVO vo;	
	private String result;
	private HttpServletRequest servletRequest;
	private AdminDAO adminDAO;
	
	public void setAdminDAO(AdminDAO adminDAO) {
		this.adminDAO = adminDAO;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}
	public void setVo(UserVO vo) {
		this.vo = vo;
	}
	public String getResult() {
		return result;
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		vo = new UserVO();
	}
	@Override
	public UserVO getModel() {
		// TODO Auto-generated method stub
		return vo;
	}	
	public String execute(){		
		try {	
			result = userDAO.verifyUser(vo);
			// 로그인 정보가 확인 되었을시(로그인 성공시)
			if(result.equals("login")){
				// 세션에 해당유저의 정보를 담은 vo를 저장한다
				ActionContext context = ActionContext.getContext();
				Map<String, UserVO> session = (Map<String, UserVO>)context.getSession();
				vo = userDAO.getUserInfo(vo.getSabun());
				session.put("user", vo);				
				context.setSession(session);						
				LoginfoVO lvo = new LoginfoVO();
				// 로그인 기록을 위해 접속자의 ip정보를 받아온다
				String ip = servletRequest.getRemoteAddr();
				// 로그인 기록VO에 사번과 ip를 저장
				lvo.setSabun(vo.getSabun());
				lvo.setLgip(ip);
				String st="1";
				lvo.setStatus("성공");
				// DB에 기록
				adminDAO.LogInput(lvo);								
				result = "success";
				//System.out.println( InetAddress.getLocalHost().getHostName() );
				//System.out.println( InetAddress.getLocalHost().getHostAddress() );			
			}
		} catch (Exception e) {
			System.out.println("UserLoginAction.execute():"+e.toString());
			result = "error";
		}
		return result;
	}
}
